package com.fy.repo.controller;

import com.fy.core.dto.PageDto;
import com.fy.core.vo.ApiRest;
import com.fy.core.vo.PageResult;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.fy.repo.service.RepoService;
import com.fy.repo.entity.Repo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 * 题库 前端控制器
 * </p>
 *
 * @author bjt
 * @since 2023-06-12
 */
@RestController
@RequestMapping("/repo")
@CrossOrigin
public class RepoController {


    @Autowired
    private RepoService repoService;

    @GetMapping(value = "/")
    public ApiRest<Page<Repo>> list(@RequestParam(required = false) Integer current, @RequestParam(required = false) Integer pageSize) {
        if (current == null) {
            current = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        Page<Repo> aPage = repoService.page(new Page<>(current, pageSize));
        return  ApiRest.success(aPage);
    }
    @GetMapping(value = "/all")
    public ApiRest<List<Repo>> all(){
        return ApiRest.success(repoService.list());
    }

    @GetMapping(value = "/{id}")
    public ApiRest<Repo> getById(@PathVariable("id") String id) {
        return  ApiRest.success(repoService.getById(id));
    }

    @PostMapping(value = "/create")
    public ApiRest<Object> create(@RequestBody Repo params) {
        repoService.save(params);
        return ApiRest.success("created successfully","");
    }

    @PostMapping(value = "/delete/{id}")
    public ApiRest<Object> delete(@PathVariable("id") String id) {
        repoService.removeById(id);
        return  ApiRest.success("deleted successfully","");
    }
    @PostMapping(value = "/delete/batch")
    public ApiRest<Object> delete(@RequestBody List<Repo> repoList){
        return repoService.batchDelete(repoList);
    }

    @PostMapping(value = "/update")
    public ApiRest<Object> delete(@RequestBody Repo params) {
        repoService.updateById(params);
        return ApiRest.success("updated successfully","");
    }
    @PostMapping(value = "/page")
    public PageResult<Repo> page(@RequestBody PageDto<Repo> params){
        return repoService.pageFind(params);
    }
}
